gtk-demo: Add a permutation to compute colors from the position
authorBenjamin Otte <otte@redhat.com>
Sun, 21 Jun 2020 02:11:24 +0000 (04:11 +0200)
committerBenjamin Otte <otte@redhat.com>
Sun, 21 Jun 2020 12:17:47 +0000 (14:17 +0200)
This way, the colors are furthest apart at the start and fill up the
remaining spots towards the end.

demos/gtk-demo/listview_colors.c

index 9ec5dc33287e0800d4ebf8f20f90f5c7b6e5f5d2..a50a180d96e8eab98918649e3a362ba487770a3a 100644 (file)
@@ -330,6 +330,32 @@ gtk_color_list_get_n_items (GListModel *list)
   return N_COLORS;
 }
 
+static guint
+position_to_color (guint position)
+{
+  static guint map[] = {
+    0xFF0000, 0x00FF00, 0x0000FF,
+    0x7F0000, 0x007F00, 0x00007F,
+    0x3F0000, 0x003F00, 0x00003F,
+    0x1F0000, 0x001F00, 0x00001F,
+    0x0F0000, 0x000F00, 0x00000F,
+    0x070000, 0x000700, 0x000007,
+    0x030000, 0x000300, 0x000003,
+    0x010000, 0x000100, 0x000001
+  };
+  guint result, i;
+
+  result = 0;
+
+  for (i = 0; i < G_N_ELEMENTS (map); i++)
+    {
+      if (position & (1 << i))
+        result ^= map[i];
+    }
+
+  return result;
+}
+
 static gpointer
 gtk_color_list_get_item (GListModel *list,
                          guint       position)
@@ -339,6 +365,8 @@ gtk_color_list_get_item (GListModel *list,
   if (position >= N_COLORS)
     return NULL;
 
+  position = position_to_color (position);
+
   if (self->colors[position] == NULL)
     {
       guint red, green, blue;